package com.fp.m7.client.view;

import com.fp.m7.client.MyMessages;
import com.fp.m7.client.presenter.LoginPresenter;
import com.fp.m7.client.widget.InfoPopup;
import com.fp.m7.shared.Constants;
import com.fp.m7.shared.ValidationResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * View for login form.
 * 
 * @author 7
 */
public class LoginView extends Composite implements LoginPresenter.Display {

    private static MyMessages msg = GWT.create(MyMessages.class);

    private FormPanel formPanel;

    TextBox tbUsername;
    PasswordTextBox tbPassword;

    public LoginView() {
        formPanel = new FormPanel();
        initWidget(formPanel);
        formPanel.setSize("284px", "112px");
        formPanel.setStyleName("fillBothDirections");
        formPanel.setAction(Constants.LOG_IN_URL);
        formPanel.setMethod("POST");

        AbsolutePanel absolutePanel = new AbsolutePanel();
        formPanel.setWidget(absolutePanel);
        absolutePanel.setSize("282px", "112px");

        Label lblUsername = new Label("Username:");
        absolutePanel.add(lblUsername, 10, 10);
        lblUsername.setSize("100px", "24px");

        tbUsername = new TextBox();
        tbUsername.setName(Constants.LOGIN_FORM_USERNAME_NAME);
        tbUsername.setSize("155px", "18px");
        absolutePanel.add(tbUsername, 116, 10);

        Label lblPassword = new Label("Password:");
        absolutePanel.add(lblPassword, 10, 44);

        tbPassword = new PasswordTextBox();
        tbPassword.setName(Constants.LOGIN_FORM_PASSWORD_NAME);
        tbPassword.setSize("155px", "18px");
        absolutePanel.add(tbPassword, 116, 44);

        Button btnNewButton = new Button("Log in!");
        absolutePanel.add(btnNewButton, 116, 78);
        btnNewButton.setSize("163px", "24px");

        tbPassword.addKeyDownHandler(new KeyDownHandler() {
            @Override
            public void onKeyDown(KeyDownEvent event) {
                if (KeyCodes.KEY_ENTER == event.getNativeKeyCode()) {
                    submit();
                }
            }
        });

        btnNewButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent evt) {
                submit();
            }
        });

        formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
            @Override
            public void onSubmitComplete(SubmitCompleteEvent evt) {
                tbPassword.setText("");
                Window.Location.replace(Constants.SECURED_URL);
            }
        });
    }

    private void submit() {
        formPanel.submit();
    }

    @Override
    public void setUsername(String username) {
        tbUsername.setText(username);
    }

    @Override
    public void clear() {
        tbUsername.setText("");
        tbPassword.setText("");
    }

    @Override
    public void showErrors(ValidationResult error) {
        if (!error.isOk()) {
            InfoPopup.showPopup(tbPassword, msg.invalidCredentials());
        }
    }
}
